<?php
/*
 * Created on Apr 1, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Category extends VNT_Controller{
		function Category(){
			parent::VNT_Controller();
			$this->load->library('xajax', 'category');
			$this->xajax->registerFunction(array('test', &$this, 'test')); 
			
			$this->xajax->processRequest();
			$this->xajax_js = $this->xajax->getJavascript($this->base_url.'js/');
			$this->checkLogin();
			$this->checkPermission();
		}
		
		function index(){
			//echo $this->config->item('language');
			//$this->main = '<div> erer erer er </div>';
			//$this->execute();
			//$this->load->view('main', '');
			$this->view();
		}
		
		function view(){
			$this->setTemplate(array ('ncategory_list.tpl'));
			$this->main = eval ($this->loadTemplate('ncategory_list.tpl'));
			$this->execute();
		}
			
		function save_product_category(){ 
			$code = trim($this->input->post('txtCode'));
			$code_s = $this->session->userdata('code');
			//echo $code  . '.............'. $code_s;
			//if($code == $code_s){ 
				$txtID = (int)$this->input->post('txtID');
				$ddlParentCategory = (int)$this->input->post('ddlParentCategory');
				$txtNameVi = trim($this->input->post('txtNameVi'));
				$txtNameEn = trim($this->input->post('txtNameEn'));
				$txtDescriptionVi = trim($this->input->post('txtDescriptionVi'));
				$txtDescriptionEn = trim($this->input->post('txtDescriptionEn'));
				$cbActive = $this->input->post('cbActive');
				$cbDeletePicture = $this->input->post('cbDeletePicture');
				
				$error = false;
				$error_message = $this->lang->line('error') . ':<br>';
				
				$config['upload_path'] = './data/categories/product/'; 
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$config['max_size']	= '1000';
				$config['max_width']  = '980';
				$config['max_height']  = '768';
				
				$this->load->library('upload', $config);
				$this->upload->initialize($config);
		
				if($txtNameVi == ''){
					$error = true; 
					$error_message .= $this->lang->line('vietnamese_name_is_required') . '<br>';
				}
				
				if($txtNameEn == ''){
					$error = true; 
					$error_message .= $this->lang->line('english_name_is_required') . '<br>';
				}
				
				$this->load->model('product_category_model', 'category', true);
				
				if($error){
					if($txtID) $template_name = 'edit_product_category.tpl';
					else $template_name = 'create_product_category.tpl';
					
					$error_message = $this->loadErrorMessage($error_message);
					$cbActive == 1 ? 'checked' : '';
					$this->setTemplate($template_name);
					$category_parent = $this->buidDDProductCategoryList('ddlParentCategory', $ddlParentCategory);
					$content = eval($this->loadTemplate($template_name));
					$this->execute($content);
					return;
				}
				else{
					$category = new stdClass();
					$category->category_id = 0;
					$category->picture = '';
					
					if($txtID){
						$category = $this->category->getById($txtID);
						if(!$category){
							redirect('category/view_product_category');
							return;
						}
						
						if($cbDeletePicture AND $category->picture != ''){
							@unlink('./data/categories/product/'.$category->picture);
							$category->picture = '';
						}
					}
					
										
					$category->category_parent_id = $ddlParentCategory;
					$category->name_vi = $txtNameVi;
					$category->name_en = $txtNameEn;
					$category->description_vi = $txtDescriptionVi;
					$category->description_en = $txtDescriptionEn;
					$category->position = $this->category->getMaxPosition($ddlParentCategory) + 1;
					$category->active = $cbActive == true ? 1 : 0;
					
					if ($this->upload->do_upload("txtFile")){
					
						if($txtID AND $category->picture != ''){
							@unlink('./data/categories/product/'.$category->picture);
						}
						
						$data = array('upload_data' => $this->upload->data());
						$category->picture = $this->upload->file_name;
						
					}
					else{
						// nothing
					}
					
					if($txtID){
						$this->category->update($category); 
					}
					else
						$this->category->create($category);
						
					$this->session->unset_userdata('code');
				}
				
			//}
			
			redirect('category/view_product_category');
		}
		
		function save_news_category(){ 
			$code = trim($this->input->post('txtCode'));
			$code_s = $this->session->userdata('code');
			//echo $code  . '.............'. $code_s;
			//if($code == $code_s){ 
				$txtID = (int)$this->input->post('txtID');
				$ddlParentCategory = (int)$this->input->post('ddlParentCategory');
				$txtNameVi = trim($this->input->post('txtNameVi'));
				$txtNameEn = trim($this->input->post('txtNameEn'));
				$txtDescriptionVi = trim($this->input->post('txtDescriptionVi'));
				$txtDescriptionEn = trim($this->input->post('txtDescriptionEn'));
				$cbActive = $this->input->post('cbActive');
				$cbDeletePicture = $this->input->post('cbDeletePicture');
				
				$error = false;
				$error_message = $this->lang->line('error') . ':<br>';
				
				$config['upload_path'] = './data/categories/news/'; 
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$config['max_size']	= '1000';
				$config['max_width']  = '980';
				$config['max_height']  = '768';
				
				$this->load->library('upload', $config);
				$this->upload->initialize($config);
		
				if($txtNameVi == ''){
					$error = true; 
					$error_message .= $this->lang->line('vietnamese_name_is_required') . '<br>';
				}
				
				if($txtNameEn == ''){
					$error = true; 
					$error_message .= $this->lang->line('english_name_is_required') . '<br>';
				}
				
				$this->load->model('news_category_model', 'category', true);
				
				if($error){
					if($txtID) $template_name = 'edit_news_category.tpl';
					else $template_name = 'create_news_category.tpl';
					
					$error_message = $this->loadErrorMessage($error_message);
					$cbActive == 1 ? 'checked' : '';
					$this->setTemplate($template_name);
					$category_parent = $this->buidDDProductCategoryList('ddlParentCategory', $ddlParentCategory);
					$content = eval($this->loadTemplate($template_name));
					$this->execute($content);
					return;
				}
				else{
					$category = new stdClass();
					$category->category_id = 0;
					$category->picture = '';
					
					if($txtID){
						$category = $this->category->getById($txtID);
						if(!$category){
							redirect('category/view_news_category');
							return;
						}
						
						if($cbDeletePicture AND $category->picture != ''){
							@unlink('./data/categories/news/'.$category->picture);
							$category->picture = '';
						}
					}
					
										
					$category->category_parent_id = $ddlParentCategory;
					$category->name_vi = $txtNameVi;
					$category->name_en = $txtNameEn;
					$category->description_vi = $txtDescriptionVi;
					$category->description_en = $txtDescriptionEn;
					$category->position = $this->category->getMaxPosition($ddlParentCategory) + 1;
					$category->active = $cbActive == true ? 1 : 0;
					
					if ($this->upload->do_upload("txtFile")){
					
						if($txtID AND $category->picture != ''){
							@unlink('./data/categories/news/'.$category->picture);
						}
						
						$data = array('upload_data' => $this->upload->data());
						$category->picture = $this->upload->file_name;
						
					}
					else{
						// nothing
					}
					
					if($txtID){
						$this->category->update($category); 
					}
					else
						$this->category->create($category);
						
					$this->session->unset_userdata('code');
				}
				
			//}
			
			redirect('category/view_news_category');
		}
		
		function create_product_category(){
			$this->setTemplate('create_product_category.tpl');
			$code = uniqid();
			$this->session->set_userdata('code', $code);
			$txtID = 0;
			$txtNameVi = '';
			$txtNameEn = '';
			$txtDescriptionVi = '';
			$txtDescriptionEn = '';
			$cbActive = 'checked';
			$error_message =''; // $this->loadErrorMessage('Thong diep loi.');
			$category_parent = $this->buidDDProductCategoryList('ddlParentCategory', 0);
			$content = eval($this->loadTemplate('create_product_category.tpl'));
			$this->execute($content);
		}
		
		function edit_product_category($id){
			
			$this->setTemplate('edit_product_category.tpl');
			$code = uniqid();
			$this->session->set_userdata('code', $code);
			$this->load->model('product_category_model', 'category', true);
			$category = $this->category->getById($id);
			
			if(!$category){
				redirect('category/view_product_category');
			}
			
			$txtID = $category->category_id;
			$txtNameVi = $category->name_vi;
			$txtNameEn = $category->name_en;
			$txtDescriptionVi = $category->description_vi;
			$txtDescriptionEn = $category->description_en;
			$cbActive = $category->active == 1 ? 'checked' : '';
			$error_message = '';
			$picture = $category->picture == "" ? "" : "<img src='".$this->base_url."data/categories/product/{$category->picture}' width=150 height=50";
			$category_parent = $this->buidDDProductCategoryList('ddlParentCategory', $category->category_parent_id);
			$content = eval($this->loadTemplate('edit_product_category.tpl'));
			$this->execute($content);
		}
		
		function edit_news_category($id){
		
			$this->setTemplate('edit_news_category.tpl');
			$code = uniqid();
			$this->session->set_userdata('code', $code);
			$this->load->model('news_category_model', 'category', true);
			$category = $this->category->getById($id);
			
			if(!$category){
				redirect('category/view_news_category');
			}
			
			$txtID = $category->category_id;
			$txtNameVi = $category->name_vi;
			$txtNameEn = $category->name_en;
			$txtDescriptionVi = $category->description_vi;
			$txtDescriptionEn = $category->description_en;
			$cbActive = $category->active == 1 ? 'checked' : '';
			$error_message = '';
			$picture = $category->picture == "" ? "" : "<img src='".$this->base_url."data/categories/news/{$category->picture}' width=150 height=50";
			$category_parent = $this->buidDDProductCategoryList('ddlParentCategory', $category->category_parent_id);
			$content = eval($this->loadTemplate('edit_news_category.tpl'));
			$this->execute($content);
		}
		
		function view_news_category(){
		
			$this->load->model('news_category_model', 'category', true);
			$categories = $this->category->getAll();
			
			$this->setTemplate(array('news_categories.tpl', 'news_categories_item.tpl'));
			$items = $this->loadNewsItems($categories);
			$content = eval($this->loadTemplate('news_categories.tpl'));
			$this->execute($content);
		}
		
		function loadNewsItems($categories = null, $parent = 0, $track = array(), $style = 1, $index = 1){
			$items = '';
			if(!$categories){
				$categories = $this->category->getAll();
			}
			$tmp = array();
			for($i = 0; $i <count($categories); $i++){
				$category = $categories[$i];
				$tmp[$category->category_parent_id][$category->category_id] = $category;
			}
			
			
			if(isset($tmp[$parent])){
				$num_record = count($tmp[$parent]);
				foreach($tmp[$parent] as $k => $v){
					$parent = $v->category_id;
					$category_id = $v->category_id;
					$category_name = $v->name_vi;
					if($index%2 == 0) $class = 'class="bg"';
					else $class = '';
					$items .= eval($this->loadTemplate('news_categories_item.tpl'));
					//if(in_array($v->category_id, $track))
					$index++;
					$items .=$this->loadProductItems($categories , $parent, $track, $style + 1, $index);
				}
				
				/*for($j = 1; $j <= count($tmp[$parent]); $j++){
					if(isset($tmp[$parent][$j])){
						$category = $tmp[$parent][$j]; 
						$parent = $category->category_id;
						$category_id = $category->category_id;
						$category_name = $category->name;
						if($index%2 == 0) $class = 'class="bg"';
						else $class = '';
						$items .= eval($this->loadTemplate('product_categories_item.tpl'));
						//if(in_array($v->category_id, $track))
						$index++;
						$items .=$this->loadProductItems($categories , $parent, $track, $style + 1, $index);
					}
				}*/
			}
			//print_r($categories);
			return $items;
		}
		
		function view_product_category(){
			$this->load->model('product_category_model', 'category', true);
			$categories = $this->category->getAll();
			$items = $this->loadProductItems($categories);
			$content = eval($this->loadTemplate('product_categories.tpl'));
			$this->execute($content);
		}
		
		function loadProductItems($categories = null, $parent = 0, $track = array(), $style = 1, $index = 1){
			$items = '';
			if(!$categories){
				$categories = $this->category->getAll();
			}
			$tmp = array();
			for($i = 0; $i <count($categories); $i++){
				$category = $categories[$i];
				$tmp[$category->category_parent_id][$category->category_id] = $category;
			}
			
			
			if(isset($tmp[$parent])){
				$num_record = count($tmp[$parent]);
				foreach($tmp[$parent] as $k => $v){
					$parent = $v->category_id;
					$category_id = $v->category_id;
					$category_name = $v->name_vi;
					if($index%2 == 0) $class = 'class="bg"';
					else $class = '';
					$items .= eval($this->loadTemplate('product_categories_item.tpl'));
					//if(in_array($v->category_id, $track))
					$index++;
					$items .=$this->loadProductItems($categories , $parent, $track, $style + 1, $index);
				}
				
				/*for($j = 1; $j <= count($tmp[$parent]); $j++){
					if(isset($tmp[$parent][$j])){
						$category = $tmp[$parent][$j]; 
						$parent = $category->category_id;
						$category_id = $category->category_id;
						$category_name = $category->name;
						if($index%2 == 0) $class = 'class="bg"';
						else $class = '';
						$items .= eval($this->loadTemplate('product_categories_item.tpl'));
						//if(in_array($v->category_id, $track))
						$index++;
						$items .=$this->loadProductItems($categories , $parent, $track, $style + 1, $index);
					}
				}*/
			}
			//print_r($categories);
			return $items;
		}
		
		function buidDDProductCategoryList($name, $default){
			$this->load->model('product_category_model', 'category', true);
			$categories = $this->category->getAll();
			$this->setTemplate(array('option_ddl.tpl', 'ddl.tpl'));
			$space = '';
			$name = 'Parent';
			$value = 0;
			$selected = '';
			$options = eval($this->loadTemplate('option_ddl.tpl')) . $this->getCategoryOption($categories, 0, $default);
			return eval($this->loadTemplate('ddl.tpl'));
		}
		
		function buidDDNewsCategoryList($name, $default){
			$this->load->model('news_category_model', 'category', true);
			$categories = $this->category->getAll();
			$this->setTemplate(array('option_ddl.tpl', 'ddl.tpl'));
			$space = '';
			$name = 'Parent';
			$value = 0;
			$selected = '';
			$options = eval($this->loadTemplate('option_ddl.tpl')) . $this->getCategoryOption($categories, 0, $default);
			return eval($this->loadTemplate('ddl.tpl'));
		}
		
		function getCategoryOption($categories = null, $parent = 0, $default = 0, $space = '&nbsp;'){
			$items = '';
			if(!$categories){
				$categories = $this->category->getAll();
			}
			$tmp = array();
			for($i = 0; $i <count($categories); $i++){
				$category = $categories[$i];
				$tmp[$category->category_parent_id][$category->category_id] = $category;
			}
						
			if(isset($tmp[$parent])){
				$num_record = count($tmp[$parent]);
				foreach($tmp[$parent] as $k => $v){
					$parent = $v->category_id;
					$value = $v->category_id;
					$name = $v->name_vi;
					$selected = '';
					if($value == $default) $selected = 'selected';
					$items .= eval($this->loadTemplate('option_ddl.tpl'));
					
					$items .=$this->getCategoryOption($categories , $parent, $default, $space . '&nbsp;&nbsp;&nbsp;');
				}
			}
			
			return $items;
		}
		
		function newcat(){
			//$this->setTemplate('form_new_ncategory.tpl');
			$code = uniqid();
			$this->session->set_userdata('code', $code);
			$txtID = 0;
			$txtViName = '';
			$txtEnName = '';
			$txtViDescription = '';
			$txtEnDescription = '';
			$txtMetaDescription = '';
			$txtMetaTitle = '';
			$txtFriendlyURL = '';
			$txtMetaKeywords = '';
			$cbActive = 'checked';
			$error_message =''; // $this->loadErrorMessage('Thong diep loi.');
			$category_parent = ''; //$this->buidDDNewsCategoryList('ddlParentCategory', 0);
			$data = array();
			
			$this->main = $this->load->view('admin/form_new_ncategory', $data, true);//eval($this->loadTemplate('form_new_ncategory.tpl'));
			$this->execute();
		}
		
		function save(){
			//$this->setTemplate('form_new_ncategory.tpl');
			
			$this->load->helper(array('form', 'url'));
		
		$this->load->library('form_validation');
			
		$this->form_validation->set_rules('txtViName', 'lang:vi_name', 'required|min_length[2]|max_length[120]');
		$this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[2]|max_length[120]');
		$this->form_validation->set_rules('txtFriendlyURL', 'lang:friendly_url', 'callback_friendly_url_check');
		/*$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required');
			*/		
		if ($this->form_validation->run() == FALSE)
		{
			//echo validation_errors();
		}
		
			$code = uniqid();
			$this->session->set_userdata('code', $code);
			$txtID = 0;
			$txtViName = trim($this->input->post('txtViName'));
			$txtEnName = trim($this->input->post('txtEnName'));
			$txtViDescription = trim($this->input->post('txtViDescription'));
			$txtEnDescription = trim($this->input->post('txtEnDescription'));
			$txtMetaDescription = trim($this->input->post('txtMetaDescription'));
			$txtMetaTitle = trim($this->input->post('txtMetaTitle'));
			$txtFriendlyURL = trim($this->input->post('txtFriendlyURL'));
			$txtMetaKeywords = trim($this->input->post('txtMetaKeywords'));
			$cbActive = 'checked';
			$error_message =''; // $this->loadErrorMessage('Thong diep loi.');
			$category_parent = ''; //$this->buidDDNewsCategoryList('ddlParentCategory', 0);
			$data = array();
			
			$data['parent_category'] = $txtViName;
			$data['error_message'] = '';
			$data['txtViName'] = $txtViName;
			$data['txtEnName'] = $txtEnName;
			$data['txtViDescription'] = $txtViDescription;
			$data['txtEnDescription'] = $txtEnDescription;
			$data['cbActive'] = $cbActive;
			$data['txtMetaDescription'] = $txtMetaDescription;
			$data['txtMetaTitle'] = $txtMetaTitle;
			$data['txtFriendlyURL'] = $txtFriendlyURL;
			$data['txtMetaKeywords'] = $txtMetaKeywords;
			//$data['cbActive'] = $cbActive;
			
		
		
			//$this->form_validation->set_rules('first_name', 'lang:first_name', 'required');
			
			$this->main = $this->load->view('admin/form_new_ncategory', $data, true);//eval($this->loadTemplate('form_new_ncategory.tpl'));
			$this->execute();
		}
		
		function friendly_url_check($url){
			if ($url == 'test'){ 
				$this->form_validation->set_message('friendly_url_check', 'The %s field can not be the word "test"');
				return FALSE;
			}
			else{
				return TRUE;
			}
		}
	}
?>
